package controller.parameterpanels;

import java.util.ArrayList;
import java.util.List;

import model.EBenchmark;
import model.ICalculatableProcess;
import model.auxillary.Parameter;
import model.auxillary.ParameterCollection;
import view.CenterTabbedPane.EBenchmarkTab;
import view.EBenchmarkPanel;
import view.parameterpanels.OutputParameterPanel;
import controller.CtrInformationPanel;
import controller.IControllable;

/**
 * The class {@code CtrOutputParameterPanel} is a controller class
 * for every standard parameter output panel. It controls the behavior of
 * it's view.
 * @author Oliver Eckstein & Sebastian Perlwitz
 *
 */
public class CtrOutputParameterPanel implements IControllable {
	
	/**
	 * The calculation model (of all processes) of the controller.
	 */
	protected ICalculatableProcess calcModelProcess;
	
	/**
	 * The information panel controller.	
	 */
	protected CtrInformationPanel infoPanelCtr;
	
	/**
	 * The controller's view.
	 */
	protected OutputParameterPanel paramPanel;
	
	/**
     * To create a new {@code CtrOutputParameterPanel}.
     * @param calcModel the calculation model connected to the controller.
     * @param infoPanelCtr the information panel controller.  
     * @param logoPath a path to an optional logo that is shown on the
     * parameter panel.
     */
    public CtrOutputParameterPanel(
    		ICalculatableProcess calcModelProcess,
            CtrInformationPanel infoPanelCtr,
            String logoPath) {  
        this.calcModelProcess = calcModelProcess;
        this.infoPanelCtr = infoPanelCtr;
        createView(logoPath);        
    }
	
	@Override
	public EBenchmarkPanel getView() {
		return paramPanel;
	}
	
	@Override
    public void createView(String logoPath) {    	
    	
		if(calcModelProcess instanceof EBenchmark) {
			paramPanel = new OutputParameterPanel(
	    			logoPath,
	    			EBenchmarkTab.SHOW_END_RESULTS,
	    			"Gesamtergebnisse Prozesse");
		} else {
			paramPanel = new OutputParameterPanel(
	    			logoPath,
	    			EBenchmarkTab.SHOW_RESULT_OF_METHOD,
	    			"Prozessergebnisse");
		}
	}
    
    /**
     * To add parameter panels to the controller's view.
     */
    public void addParameters() {
    	
    	// Remove formerly created parameter informations.
    	paramPanel.getParameterPanel().removeAll();
    	List<ParameterCollection> results = calcModelProcess.getResults();
    	for(ParameterCollection collection : results) {
    		List<String[]> paramsInfo = new ArrayList<String[]>();
    		for(Parameter param : collection.getParameters()) {
    			// Fetch the interesting parameter information.
    			String[] paramInfo = new String[4];
    			paramInfo[0] = param.getName();
    			paramInfo[1] = param.getGuiAbbreviation();
    			paramInfo[2] = param.getUnit();
    			if(Double.isNaN(param.getValue())) {
    				paramInfo[3] = "";
    			} else {
    				paramInfo[3] = String.valueOf(
    						Math.round(param.getValue() * 100000000.0) /
    						100000000.0);
    			}
    			paramsInfo.add(paramInfo);    			
    		}
    		paramPanel.addParameterSet(paramsInfo, collection.getName());    		
    	}
    }
}
